import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo2 {

    public static BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(100);

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {


                try {
                    int n = 0;
                    while (true) {
                    blockingQueue.put(n + "");
                        System.out.println("生产元素：" + n);
                        n++;
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
        });

        Thread t2 = new Thread(() -> {
            try {
                while (true) {
                    String n = blockingQueue.take();

                    System.out.println("消费元素：" + n);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t2.start();
    }
}
